<template>
	<view class="content">
		<image class="logo" src="/static/logo.png"></image>
		<view class="text-area">
			<text class="title">{{title}}</text>
		</view>
	</view>
</template>

<script>
	import RongIMLib from '../../common/RongIMLib-3.0.6.2-dev.js'
	import Sha1 from '../../common/sha1.min.js'
	var _self;
	var navi = null
	var RongIMClient = RongIMLib
	
	//认证企业key
	// const appkey = 'pvxdm17jpor0r'
	// const appSecret = 'TcsKVSKdrj'
	
	//非认证企业key
	const appkey = 'sfci50a7sxt6i'
	const appSecret = 'efUiFx0E0y'

	var config = {}
	if (navi) {
		config.navi = navi
	}

	export default {
		data() {
			return {
				title: 'rongyun demo',
				token: ''
			}
		},
		onLoad() {
			_self = this
			this.getToken()
		},
		methods: {
			//模拟服务器端获取token
			getToken() {
				let data = {
					userId: 'testId',
					name: 'testName',
					portraitUri: 'logo.png'
				}

				let Nonce = parseInt(Math.random() * 10000000000)
				let Timestamp = parseInt(new Date().getTime() / 1000)
				
				//生成签名
				let Signature = Sha1(appSecret + Nonce + Timestamp)
				
				uni.request({
					url: 'http://api-cn.ronghub.com/user/getToken.json',
					data,
					method: "POST",
					header: {
						'App-Key': appkey,
						Nonce,
						Timestamp,
						Signature,
						"Content-Type": "application/x-www-form-urlencoded"
					},
					success: function(res) {
						console.log(res.data.token)
						console.log('模拟服务器端获取token成功')
						_self.token = res.data.token
						setTimeout(()=>{
							console.log('客户端初始化并连接融云服务器')
							_self.connectRongIM()
						},2000)
					},
					fail: (e) => {
						console.log(e)
					}
				})
			},
			//客户端连接服务器
			connectRongIM() {
				//客户端初始化
				let im = RongIMClient.init({
					appkey
				})
				//客户端连接融云
				var conversationList = []; // 当前已存在的会话列表
				im.connect({
					token: _self.token
				}).then((user) => {
					console.log('链接成功, 链接用户 id 为: ', user.id);
					console.log('开始模拟服务器端发送消息')
					_self.serverSendMessage()
					
				}).catch(function(error) {
					console.log('链接失败: ', error.code, error.msg);
				});
				
				im.watch({
					conversation: function(event) {
						var updatedConversationList = event.updatedConversationList; // 更新的会话列表
						// console.log('更新会话汇总:', updatedConversationList);
						// console.log('最新会话列表:', im.Conversation.merge({
						// 	conversationList,
						// 	updatedConversationList
						// }));
					},
					message: function(event) {
						var message = event.message;
						console.log('接收消息成功，消息内容为:', message.content.content);
					},
					status: function(event) {
						var status = event.status;
						console.log('连接状态码:', status);
					}
				});


			},
			//模拟服务器端向单个客户端发送消息
			serverSendMessage() {
				let data = {
					fromUserId:'system',
					toUserId: 'testId',
					objectName:'RC:TxtMsg',
					content:'{"content":"hello 我是服务器端发来的系统消息","extra":"helloExtra"}'
				}

				let Nonce = parseInt(Math.random() * 10000000000)
				let Timestamp = parseInt(new Date().getTime() / 1000)
				let Signature = Sha1(appSecret + Nonce + Timestamp)
				uni.request({
					url: 'http://api-cn.ronghub.com/message/system/publish.json',
					data,
					method: "POST",
					header: {
						'App-Key': appkey,
						Nonce,
						Timestamp,
						Signature,
						"Content-Type": "application/x-www-form-urlencoded"
					},
					success: function(res) {
						console.log('服务端消息发送成功')
					},
					fail: (e) => {
						console.log(e)
					}
				})
			},
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>
